﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Bid : System.Web.UI.Page
    {
        IBLLTree bllTree = new TreeBLL();
        IBLLSysYearType bllYearType = new SysYearTypeBLL();
        BidBLL bllBid = new BidBLL();
        IBLLSysRecordStatus bllStatus = new SysRecordStatusBLL();
        IBLLSysProgress bllProgress = new SysProgressBLL();
        IBLLPrePurchaseRequest bllPPR = new PrePurchaseRequestBLL();
        IBLLMasterPurchaseMethod bllMasPurchaseMethod = new MasterPurchaseMethodBLL();
        PurchaseRequestBLL bllPurchaseRequest = new PurchaseRequestBLL();
        IBLLHrmEmployee bllEmployee = new HrmEmployeeBLL();
        SysControlBLL bllSysCon = new SysControlBLL();
        static int ItemsPerRequest = 10;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDefault();
                LoadYearType();
                LoadRecordStatus();
                LoadDataGrid();
            }
        }

        protected void LoadDefault()
        {
            SysControlInfo infoDefault = bllSysCon.GetDefaultData();
            if (infoDefault.YearType != null)
            {
                cbYearType.SelectedValue = infoDefault.YearType.ToString();
            }
            if (infoDefault.Year != null)
            {
                txtYear.Text = infoDefault.Year.ToString();
            }
        }

        protected void LoadDataGrid()
        {
            Nullable<int> YearType = Convert.ToInt32(cbYearType.SelectedValue.ToString());
            Nullable<int> Year = Convert.ToInt32(txtYear.Text);
            string RecordStatus = (cbStatus.SelectedValue.ToString() != "") ? cbStatus.SelectedValue.ToString() : "";

            IList<BidInfo> infoList = bllBid.GetBid(YearType, Year, RecordStatus);
            Grid.DataSource = infoList;
            //GridGroupByExpression g;
            //Grid.MasterTableView.GroupByExpressions.Clear();
            //g = new GridGroupByExpression(Grid.Columns[1]);
            //Grid.MasterTableView.GroupByExpressions.Add(g);
            Grid.DataBind();
        }

        protected void LoadYearType()
        {
            IList<SysYearTypeInfo> infoList = bllYearType.GetSysYearType();
            cbYearType.DataSource = infoList;
            cbYearType.DataBind();
        }

        protected void LoadRecordStatus()
        {
            IList<SysRecordStatusInfo> infoList = bllStatus.GetSysRecordStatusTransaction();
            cbStatus.DataSource = infoList;
            cbStatus.DataBind();
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            Session["YearType"] = Convert.ToInt32(cbYearType.SelectedValue.ToString());
            Session["Year"] = Convert.ToInt32(txtYear.Text);
            LoadDataGrid();
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            IList<BidInfo> infoList = new List<BidInfo>();
            foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
            {
                BidInfo info = new BidInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                infoRecord.Updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                RadManager1.Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bllBid.DeleteAll(infoList);
            }
            catch
            {
                RadManager1.Alert(SystemInfo._DeleteError);
                return;
            }

            LoadDataGrid();
        }

        protected void RadManager1_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
        {
            LoadDataGrid();
        }

        protected void Grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            LoadDataGrid();
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }

        protected void cbPurchaseRequest_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<PurchaseRequestInfo> infoList = bllPurchaseRequest.GetPurchaseRequestForBid(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + ItemsPerRequest, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                cbPurchaseRequest.Items.Add(new RadComboBoxItem(infoList[i].Ref_No + " - " +  infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdRule = new TableCell();
                Label lblRule = new Label();
                lblRule.Text = infoList[i].InfoPurchaseRule.Name;
                tdRule.Width = Unit.Pixel(150);
                tdRule.Controls.Add(lblRule);
                tr.Cells.Add(tdRule);
                TableCell tdPrType = new TableCell();
                Label lblPrType = new Label();
                lblPrType.Text = infoList[i].PurchaseType;
                tdPrType.Width = Unit.Pixel(120);
                tdPrType.Controls.Add(lblPrType);
                tr.Cells.Add(tdPrType);
                TableCell tdRefNo = new TableCell();
                Label lblRefNo = new Label();
                lblRefNo.Text = infoList[i].Ref_No;
                tdRefNo.Width = Unit.Pixel(130);
                tdRefNo.Controls.Add(lblRefNo);
                tr.Cells.Add(tdRefNo);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(150);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdSumAmount = new TableCell();
                Label lblSumAmount = new Label();
                lblSumAmount.Text = infoList[i].SumAmount;
                tdSumAmount.Width = Unit.Pixel(100);
                tdSumAmount.Controls.Add(lblSumAmount);
                tr.Cells.Add(tdSumAmount);
                TableCell tdEmp = new TableCell();
                Label lblEmp = new Label();
                lblEmp.Text = infoList[i].InfoPurchaseEmp.Name;
                tdEmp.Width = Unit.Pixel(100);
                tdEmp.Controls.Add(lblEmp);
                tr.Cells.Add(tdEmp);
                ((Table)cbPurchaseRequest.Items[j].FindControl("tablePurchaseRequest")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid.MasterTableView.FilterExpression = string.Empty;
            LoadDataGrid();
        }
    }
}
